package com.wenlong.controller;

import com.wenlong.pojo.Commodity;
import com.wenlong.pojo.Page;
import com.wenlong.service.CommodiftyService;
import com.wenlong.service.impl.CommodittyServiceImpl;
import com.wenlong.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author 苏文龙
 * @version 1.0
 */
@WebServlet(name = "CommodityServlet", urlPatterns = {"/managers/commodityServlet"})
public class CommodityServlet extends BaseServlet {
    CommodiftyService cs = new CommodittyServiceImpl();

    public void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Commodity> commodities = cs.queryAllCommodity();
        req.setAttribute("commodities", commodities);
        req.getRequestDispatcher("/pages/manager/commodity_manager.jsp").forward(req, resp);
    }

    public void findCommodityById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        int id1 = WebUtils.parseInt(id, 0);
        Commodity commodity = cs.queryCommodityById(id1);
        req.setAttribute("commodity", commodity);
        req.getRequestDispatcher("/pages/manager/operationOfCommodity.jsp").forward(req,resp);
    }

    public void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Commodity commodity = WebUtils.injectParameterToBean(req.getParameterMap(), new Commodity());
        cs.updateCommodity(commodity);
        resp.sendRedirect(req.getContextPath() + "/managers/commodityServlet?action=page&pageNo="+req.getParameter("pageNo"));
    }

    public void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        int id1 = WebUtils.parseInt(id, 0);
        cs.deleteCommodity(id1);
        resp.sendRedirect(req.getContextPath() + "/managers/commodityServlet?action=page&pageNo=" + req.getParameter("pageNo"));
    }

    public void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int pageNo = WebUtils.parseInt(req.getParameter("pageNo"), 0) + 1;
        Commodity commodity = WebUtils.injectParameterToBean(req.getParameterMap(), new Commodity());
        cs.addCommodity(commodity);
        resp.sendRedirect(req.getContextPath() + "/managers/commodityServlet?action=page&pageNo="+pageNo);
    }

    public void page(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int pageNo = WebUtils.parseInt(req.getParameter("pageNo"), 1);
        int pageSize = WebUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
        Page<Commodity> page = cs.page(pageNo,pageSize);
        page.setUrl("managers/commodityServlet?action=page");
        req.setAttribute("page",page);
        req.getRequestDispatcher("/pages/manager/commodity_manager.jsp").forward(req,resp);
    }
}
